﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Common.Threads
{
    public class DelegateHelper : WaitHelper
    {
        public static Func<T> WithReturn<T>(Func<T> func)
        {
            IAsyncResult res = func.BeginInvoke(null, null);
            return () =>
            {
                res.AsyncWaitHandle.WaitOne();
                T t = func.EndInvoke(res);
                res.AsyncWaitHandle.Dispose();
                return t;
            };
        }
        public static Func<T> Callback<T>(Action act, Func<T> func)
        {
            return WithReturn(() =>
            {
                act.Invoke();
                return func.Invoke();
            });
        }
    }
}
